package com.example.bottomnavigationdemo.ui.a_home

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.example.bottomnavigationdemo.MainActivity
import com.example.bottomnavigationdemo.R

class HomeFragment : Fragment() {

    private lateinit var homeViewModel: HomeViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("-------->", "HomeFragment-onCreate")

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e("-------->", "HomeFragment-onDestroy")
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//        Log.e("-------->", "onCreateView")
        homeViewModel = ViewModelProvider(requireActivity()).get(HomeViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_home, container, false)
        val textView: TextView = root.findViewById(R.id.text_home)
        homeViewModel.text.observe(requireActivity(), Observer {
            textView.text = it
        })
        root.findViewById<Button>(R.id.text_home).setOnClickListener {
            homeViewModel.setData("wang xiao yan")
            findNavController().navigate(R.id.navigation_notifications)
        }
        root.findViewById<Button>(R.id.button3).setOnClickListener {
            (activity as MainActivity).chuanZhi()
        }
        return root
    }

    fun xx(c: String) {
        Toast.makeText(activity, "$c", Toast.LENGTH_LONG).show()
    }

}